package com.ruoyi.web.controller.admin;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.module.domain.Order;
import com.ruoyi.module.po.OrderDeliveryPo;
import com.ruoyi.module.po.OrderPo;
import com.ruoyi.module.service.IOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 订单Controller
 *
 * @author fubojin
 * @date 2024-12-06
 */
@RestController
@RequestMapping("/admin/order")
public class AdminOrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单列表(订单列表)
     */
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 查询订单列表
     */
    @GetMapping("/distributeList")
    public TableDataInfo distributeList(Order order)
    {
        startPage();
        List<Order> list = orderService.distributeList(order);
        return getDataTable(list);
    }

    /**
     * 查询订单列表（订单明细）
     */
    @GetMapping("/detailedList")
    public TableDataInfo detailedList(Order order)
    {
        startPage();
        List<Order> list = orderService.detailedList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderService.selectOrderById(id));
    }


    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order)
    {
        orderService.insertOrder(order);
        return toAjax(1);
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderService.deleteOrderByIds(ids));
    }


    /**
     * 发货
     */
    @PostMapping("setDeliveryNo")
    @ApiOperation("发货")
    @Log(title = "分销订单", businessType = BusinessType.OTHER)
    public AjaxResult setDeliveryNo(@RequestBody OrderDeliveryPo distributeOrderDeliveryPo) {
        orderService.setDeliveryNo(distributeOrderDeliveryPo);
        return success();
    }


    /**
     * 收货
     */
    @PostMapping(value = "setSignForReceipt/{id}")
    public AjaxResult setSignForReceipt(@PathVariable("id") Long id){
        orderService.setSignForReceipt(id);
        return success();
    }


    @GetMapping(value = "orderSettlement")
    @ApiOperation("订单结算")
    @Log(title = "分销订单", businessType = BusinessType.OTHER)
    public AjaxResult orderSettlement(OrderPo orderPo) {
        return success(orderService.orderSettlement(orderPo));
    }

    @GetMapping(value = "cancelOrder/{id}")
    @ApiOperation("取消发货订单")
    @Log(title = "分销订单", businessType = BusinessType.OTHER)
    public AjaxResult cancelOrder(@PathVariable("id") Long id) {
        orderService.cancelOrder(id);
        return success();
    }

    @GetMapping(value = "cancelOrderToAdopt/{id}")
    @ApiOperation("取消订单发货审核通过")
    @Log(title = "分销订单", businessType = BusinessType.OTHER)
    public AjaxResult cancelOrderToAdopt(@PathVariable("id") Long id) {
        orderService.cancelOrderToAdopt(id);
        return success();
    }
    @GetMapping(value = "cancelDeliverNotAdopt/{id}")
    @ApiOperation("取消订单发货审核不通过")
    @Log(title = "分销订单", businessType = BusinessType.OTHER)
    public AjaxResult cancelOrderNotAdopt(@PathVariable("id") Long id,@RequestParam("reasonForCancel") String reasonForCancel) {
        orderService.cancelOrderNotAdopt(id, reasonForCancel);
        return success();
    }


    @PostMapping(value = "refundOrder")
    @ApiOperation("收货后-退货退款订单")
    @Log(title = "分销订单", businessType = BusinessType.OTHER)
    public AjaxResult refundOrder(@RequestBody Order order) {
        orderService.refundOrder(order);
        return success();
    }

}
